package org.robot;

import org.game.netty.oldcoder.other.IChannelConstants;
import org.game.protobuf.s2c.S2CLoginMsg;
import org.robot.actor.ClientPlayerActor;

import com.sojoys.artifact.factory.message.IMessage;
import com.sojoys.artifact.factory.session.ISession;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;

public class EchoHandler extends ChannelInboundHandlerAdapter {

	static final Log log = LogFactory.get(EchoHandler.class);

	static final ActorSystem system = ActorSystem.create("LocalSystem");

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		super.channelRead(ctx, msg);
		if (msg instanceof IMessage) {
			IMessage imsg = (IMessage) msg;
			Attribute<ISession> session = ctx.channel().attr(IChannelConstants.SESSION);
			ISession isession = session.get();
			if (imsg.getCommandId() == 257) {
				S2CLoginMsg.EnterSuc resp = S2CLoginMsg.EnterSuc.parseFrom(imsg.getBody());
				log.info("******************{}-{} 登陆成功 ******************",resp.getPlayerinfo().getPid(), resp.getPlayerinfo().getName());
				ActorRef actorRef = system.actorOf(Props.create(ClientPlayerActor.class, isession),"ClientPlayer-" + resp.getPlayerinfo().getPid());
				isession.setAttr("actor", actorRef);
				// 加载玩家数据
				actorRef.tell("loadPlayer", ActorRef.noSender());
			}else {
				// 接收数据
				ActorRef actorRef = isession.getAttr("actor");
				actorRef.tell(imsg, ActorRef.noSender());
//				
//				// 这将会计划5s后每10s向tickActor发送 Tick-消息
//				system.scheduler().schedule(Duration.create(5, TimeUnit.SECONDS),Duration.create(10, TimeUnit.SECONDS), actorRef, "trigger",system.dispatcher(), null);
			}
		}
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelReadComplete(ctx);
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelRegistered(ctx);
	}

	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelUnregistered(ctx);
	}

	@Override
	public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelWritabilityChanged(ctx);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// TODO Auto-generated method stub
		super.exceptionCaught(ctx, cause);
	}

	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		// TODO Auto-generated method stub
		super.userEventTriggered(ctx, evt);
	}

}
